//
//  Task_GetInfoListWithPic.m
//  wmh.iphone.shhk
//
//  Created by andli on 14-12-12.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "Task_GetInfoListWithPic.h"
#import "SHHK_InfoListModel.h"

@implementation Task_GetInfoListWithPic

-(void)execute
{
    //    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *url = NSLocalizedString(@"Epoint_DNet_WMH", @"");
    NSString *ValidateData = [EpointBizTools getToken];
    NSString *namespace = NSLocalizedString(@"Epoint_DNet_WMH_NameSpace", @"");
    
    url = @"https://www.shhk.gov.cn/shhksjk/WebBuilderMobileService/Service.asmx";
    
    NSString *CurrentPageIndex = [params objectForKey:@"CurrentPageIndex"];
    NSString *PageSize = [params objectForKey:@"PageSize"];
    NSString *CateNum = [params objectForKey:@"CateNum"];
    NSString *IsHeadNews = [params objectForKey:@"IsHeadNews"];
    NSString *Title = [params objectForKey:@"Title"];
    
    //请求接口
    NSString *method = @"GetInfoListWithPic";
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><CurrentPageIndex>%@</CurrentPageIndex><PageSize>%@</PageSize><CateNum>%@</CateNum><IsHeadNews>%@</IsHeadNews><Title>%@</Title></paras>]]>";
    ParasXml = [NSString stringWithFormat:ParasXml,CurrentPageIndex,PageSize,CateNum,IsHeadNews,Title];
    
    NSString *key = [NSString stringWithFormat:@"%@%@%@%@",method,CateNum,PageSize,CurrentPageIndex];

    //请求接口
    EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:namespace method:method];
    [ws addParameterForString:@"ValidateData" value:ValidateData];
    [ws addParameterForString:@"ParasXml" value:ParasXml];
    NSString *bs =  ws.start;
    
    NSMutableArray *result = [self validateXMLWS2:bs];
    NSString *msg = [result objectAtIndex:1];
    if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
        [DBFrameService setCacheValue:key value:bs];
        [self exeSuccessWithData:bs];
    }
    else
    {
        [self userCache:key message:msg];
    }
    
}

-(void)userCache:(NSString *)key message:(NSString *)msg
{
    //获取缓存数据
    NSString *cacheBS = [DBFrameService getCacheValue:key];
    
    //判断是否存在缓存
    if (cacheBS.length>0) {
        [self exeSuccessWithData:cacheBS];
    }
    else
    {
        [self exeFailureWithMessage:msg];
    }
}

#pragma mark 解析XML
+(NSMutableArray *) getLists:(NSString *)result
{
    NSString *UserArea = [EpointStringUtil getXMLFormatedAttributeBsWithTag:result xmlTag:@"InfoList"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:UserArea  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"Info"];
    NSMutableArray *dataList = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays) {
        SHHK_InfoListModel *f = [[SHHK_InfoListModel alloc]init];
        GDataXMLElement *Infoide = [[arr elementsForName:@"InfoID"] objectAtIndex:0];
        f.InfoID = [Infoide stringValue];
        GDataXMLElement *titleE = [[arr elementsForName:@"Title"] objectAtIndex:0];
        f.Title = [titleE stringValue];
        GDataXMLElement *InfoDate = [[arr elementsForName:@"InfoDate"] objectAtIndex:0];
        f.InfoDate = [InfoDate stringValue];
        GDataXMLElement *IsAttach = [[arr elementsForName:@"IsAttach"] objectAtIndex:0];
        f.IsAttach = [IsAttach stringValue];
        GDataXMLElement *HeadNewsAttachGuid = [[arr elementsForName:@"HeadNewsAttachGuid"] objectAtIndex:0];
        f.HeadNewsAttachGuid = [HeadNewsAttachGuid stringValue];
        GDataXMLElement *ClickTimes = [[arr elementsForName:@"ClickTimes"] objectAtIndex:0];
        f.ClickTimes = [ClickTimes stringValue];
        [dataList addObject:f];
    }
    return dataList;
}
// <InfoList>
// <Info>
// <InfoID>c0c0869a-a68e-4d25-be14-e6190f3268a1</InfoID>
// <Title><![CDATA[陈正邦视察我区造林绿化工作]]></Title>
// <InfoDate>2014-03-27 03:00</InfoDate>
// <IsAttach>1</IsAttach>
// <HeadNewsAttachGuid>3617b639-dd75-42ce-b457-57ce0c9d02ac</HeadNewsAttachGuid>
// </Info>


@end
